package com.example.lay.service;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.Toast;

/**
 * Created by Lay on 2014/12/9.
 */
public class MyBindServiceActivity extends Activity {

    private final String ACTION = "www.lalio.xyz.BIND";
    private final Intent intent = new Intent();

    {
        intent.setAction(ACTION);
    }

    private MyBindService.MyBinder binder;
    private ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            binder = (MyBindService.MyBinder) service;
            System.out.println("Service Connected");
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            System.out.println("Service DisConnected");

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.acvitity_bing_service);
    }

    public void bind(View view) {
        bindService(intent, conn, BIND_AUTO_CREATE);
    }

    public void unbind(View view) {
        unbindService(conn);
    }

    public void getStatus(View view) {
        Toast.makeText(this, "Service Count:" + binder.getCount(), Toast.LENGTH_SHORT).show();
    }
}
